package app.view.mediators
{
	import app.AppConstants;
	import app.view.forms.TextForm;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class TextFormMediator extends Mediator
	{
		public static const NAME:String = "TextFormMediator";
		private var application:Application;
		private var text:String;
		private var isCreated:Boolean;
		
		public function TextFormMediator(application:Application){
			this.application = application;
			super(NAME, new TextForm());
			view.addEventListener(MouseEvent.CLICK, onClick);
			view.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
		}
		
		
		override public function listNotificationInterests():Array{
			return [	AppConstants.SHOW_TEXT_FORM
											]
		}
		
		override public function handleNotification(note:INotification):void{
			
			switch( note.getName() ){
					
				case AppConstants.SHOW_TEXT_FORM:
					text = note.getType()
					show();
					break;
				
			}
		}
		
		public function onClick(event:MouseEvent):void{
			var target:Button = event.target as Button;
			if(!target)return
			switch(event.target.id){
				case"okBtn":
					hide();
					break;
			}
		}
		
		public function onCreationComplete(event:FlexEvent):void{
			view.removeEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
			setText();
			isCreated = true;
		}
		
		private function setText():void{
			
				view.tArea.text = text;
			
		}
		
		public function show(  ):void{
			
			PopUpManager.addPopUp( view, application, true );
			PopUpManager.centerPopUp( view );
			if(isCreated)setText();
		}
		
		
		public function hide(  ):void{
			
			PopUpManager.removePopUp( view );
			
		}
		
		
		private function get view():TextForm{
			return TextForm(viewComponent);
		}
		
	}
}